package com.jingdianjichi.wx.controller;

import com.jingdianjichi.wx.handler.WxChatMsgFactory;
import com.jingdianjichi.wx.handler.WxChatMsgHandler;
import com.jingdianjichi.wx.utils.MessageUtil;
import com.jingdianjichi.wx.utils.SHA1;
import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.message.Message;
import org.apache.maven.shared.utils.logging.MessageUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * ClassName:CallBackController
 * Package:com.jingdianjichi.wx
 * Description:
 *
 * @Author fst
 * @Create 2024/10/15 19:13
 * @Version 1.0
 */
@RestController
@Slf4j

public class CallBackController
{

    private static String token="dafdafdahsfhf";
    @RequestMapping("test")
    public String test()
    {
        return "test";
    }

    @Resource
    private WxChatMsgFactory wxChatMsgFactory;

    //打通微信公众号
    @GetMapping("callback")
    public String callback(@RequestParam("signature") String signature,@RequestParam("timestamp")
    String timestamp,@RequestParam("nonce") String nonce, @RequestParam("echostr") String  echostr)
    {
        log.info("get验签请求参数：signature:{},timestamp:{},nonce:{},echostr:{}",signature,timestamp,nonce,echostr);
        String sha1Str = SHA1.getSHA1(token, timestamp, nonce, "");

        if(signature.equals(sha1Str))
        {
            return echostr;
        }
        else return "unkown";
    }

    @PostMapping(value = "callback",produces = "application/xml;charset=UTF-8")
    public String callback(
            @RequestBody String requestBody,
            @RequestParam("signature") String signature,
            @RequestParam("timestamp") String timestamp,
            @RequestParam("nonce") String nonce,
            @RequestParam(value = "msg_signature",required = false)String msg_signature)
    {
        log.info("验签请求参数： requstbody:{}, signature:{},timestamp:{},nonce:{}",requestBody,signature,timestamp,nonce);

        Map<String,String> msgmap= MessageUtil.parseXml(requestBody);

        String toUserName=msgmap.get("ToUserName");
        String fromUserName=msgmap.get("FromUserName");

        String msgType=msgmap.get("MsgType");
        String event = msgmap.get("Event")==null ? "": msgmap.get("Event");

        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(msgType);
        if(!StringUtils.isEmpty(event))
        {
            stringBuilder.append(".");
            stringBuilder.append(event);
        }

        String msgTypeKey=stringBuilder.toString();

        log.info("msgTypeKey:{}",msgTypeKey);

        WxChatMsgHandler wxChatMsgHandler = wxChatMsgFactory.getHandlerByMsgType(msgTypeKey);


        if(Objects.isNull(wxChatMsgHandler))
        {
            return "unkown";
        }

        String msg=wxChatMsgHandler.dealMsg(msgmap);

       return msg;
    }
//<xml><ToUserName><![CDATA[gh_d0e4b7ee5b2c]]></ToUserName>
//<FromUserName><![CDATA[op0VI6ava6wkkJuTJxv_SRRQGDZ0]]></FromUserName>
//<CreateTime>1728994948</CreateTime>
//<MsgType><![CDATA[event]]></MsgType>
//<Event><![CDATA[subscribe]]></Event>
//<EventKey><![CDATA[]]></EventKey>
//</xml>
}
